package com.easy.slurm.controller;

import com.easy.slurm.slurm.api.SlurmApi;
import com.easy.slurm.slurm.bean.CancelJobRequest;
import com.easy.slurm.slurm.bean.SbatchJobSubmit;
import com.easy.slurm.vo.ResultVo;
import com.jcraft.jsch.JSchException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author bing.bai
 * @create 2025/5/30
 */
@Slf4j
@RestController
@RequestMapping("slurm/jobs")
public class JobController {

    
    @Autowired
    private SlurmApi slurmApi;


    /**
     * 提交批作业
     * @param sbatchJobSubmit
     * @return
     * @throws JSchException
     * @throws IOException
     */
    @PostMapping("/submit")
    public ResultVo submit(@RequestBody SbatchJobSubmit sbatchJobSubmit) throws JSchException, IOException {
        return slurmApi.submitSbatchJob(sbatchJobSubmit);
    }

    /**
     * 作业队列
     * @return
     * @throws JSchException
     * @throws IOException
     */
    @GetMapping("/squeue")
    public ResultVo squeue() throws JSchException, IOException {
        return ResultVo.data(slurmApi.squeue());
    }

    /**
     * 取消作业
     * @param cancelJobRequest
     * @return
     * @throws JSchException
     * @throws IOException
     */
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestBody CancelJobRequest cancelJobRequest) throws JSchException, IOException {
        return slurmApi.cancel(cancelJobRequest);
    }

    /**
     * 作业详情
     * @param jobId
     * @return
     * @throws JSchException
     * @throws IOException
     */
    @GetMapping("/scontrol")
    public ResultVo scontrol(String jobId) throws JSchException, IOException {
        return ResultVo.data(slurmApi.scontrol(jobId));
    }


}
